package cn.chenlh.fx.pojo;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.List;
import java.util.function.Function;

public class ListItem<T> {
    T data;
    Function<T, String> labelGet;

    private ListItem() {
    }

    public static <V> ListItem<V> of(V data, Function<V, String> labelGet) {
        ListItem<V> res = new ListItem<>();
        res.data = data;
        res.labelGet = labelGet;
        return res;
    }

    public static <R> ObservableList<ListItem<R>> listOf(List<R> data, Function<R, String> labelGet) {
        ObservableList<ListItem<R>> list = FXCollections.observableArrayList();
        for (R d : data) {
            list.add(ListItem.of(d, labelGet));
        }
        return list;
    }
    @SafeVarargs
    public static <R> ObservableList<ListItem<R>> listOf(Function<R, String> labelGet, R... data) {
        ObservableList<ListItem<R>> list = FXCollections.observableArrayList();
        for (R d : data) {
            list.add(ListItem.of(d, labelGet));
        }
        return list;
    }

    public T getData() {
        return data;
    }

    public String getLabel() {
        return labelGet.apply(data);
    }

    @Override
    public String toString() {
        return data.toString();
    }
}
